#include "iniparser.h"
#include <fstream>
#include <iostream>

using namespace MyIni;

/** 去除左右边的空格 */
static const std::string& strip(std::string& str) 
{
    if (str.empty())
        return str;
    
    str.erase(0, str.find_first_not_of(" "));
    str.erase(str.find_last_not_of(" ") + 1);

    return str;
}


ConfigIni::ConfigIni(const std::string& filename):m_filename(filename)
{
    std::ifstream ifs(filename.c_str(), std::ios::binary); 
    
    if (!ifs) 
    {
        std::cout << " open error" << std::endl;
        exit(-1);
    }
    std::string line;
    std::string key, kkey, val;
    int pos;
    while (getline(ifs, line))
	{
        line = strip(line);
        // 判断是否为空行
        if (line == "") continue;
        // 判断是否为注释
        if (line[0] == '#') continue;

        // 判断是否为节
        if (line[0] == '[') 
        {
            key = line.substr(1, line.size() - 2);
            key = strip(key);

            pos = key.find(']');
            if (pos != std::string::npos)
                key = key.substr(0, pos); 

        // 处理键值
        } else {
            line = strip(line);

            pos = line.find("=");
            if (pos == std::string::npos) {

                std::cout << "[error]: " << kkey << std::endl;
                continue;
            }

            kkey = line.substr(0, pos);
            kkey = strip(kkey);
            val = line.substr(pos + 1, line.size() - pos); 
            val = strip(val);

            do {
                /** 处理单引号 */
                if (val[0] == '\'') 
                {
                    if (val.size() != 2 && val == std::string(val.size(), '\'')) 
                        break;
                    pos = val.find('\'', 1);
                    if (pos != std::string::npos) 
                    {
                        val = val.substr(1, pos - 1);
                        break;
                    }
                }
                /** 处理双引号 */
                if (val[0] == '\"') 
                {
                    if (val.size() != 2 && val == std::string(val.size(), '\"')) 
                        break;
                    pos = val.find('\"', 1);
                    if (pos != std::string::npos) 
                    {
                        val = val.substr(1, pos - 1);
                        break;
                    }
                }
                pos = val.find(';');
                if (pos != std::string::npos) 
                    val = val.substr(0, pos); 

            } while(0);

            val = strip(val);
            m_dict[key][kkey] = val;
            std::cout << key << ":" << kkey << ":" << val << std::endl;
        }
	}
}

ConfigIni::~ConfigIni() {}

void ConfigIni::printIni()
{
    if (m_dict.empty()) 
        return;

    for (auto it = m_dict.begin(); it != m_dict.end(); it++)
    {
        if (it->second.empty())
            continue;

        for (auto iter = it->second.begin(); iter != it->second.end(); iter++)
        {
            std::cout << it->first << ":" << iter->first << ":" << iter->second << std::endl;
        }
    }

}
